package myLinkList;

public class MyLinkList implements IMyLinkList {
    static class ListNode {
        private int val;
        private ListNode pre;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    ListNode head;
    ListNode last;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = last = node;
        } else {
            node.next = head;
            head.pre = node;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = last = node;
        } else {
            last.next = node;
            node.pre = last;
            last = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        //需要插入的节点
        ListNode node = new ListNode(data);
        //判断index是否合法
        if (index < 0 || index > size()) {
            throw new IndexException("下标 " + index + " 不合法");
        }
        //第0个位置头插
        if (index == 0) {
            addFirst(data);
            return;
        }
        //最后一个位置尾插
        if (index == size()) {
            addLast(data);
            return;
        }
        //中间插入
        //找到需要插入的位置
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        //此时cur位置就是需要插入的位置
        cur.pre.next = node;
        node.next = cur;
        node.pre = cur.pre;
        cur.pre = node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        //找到对应关键字
        while (cur != null) {
            if (cur.val == key) {
                //需要删除的cur在头节点
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.pre = null;
                    } else {
                        last = null;
                    }
                }else {
                    //删除中间节点
                    if(cur.next != null) {
                        cur.pre.next = cur.next;
                        cur.next.pre = cur.pre;
                    }else {
                        //删除尾节点
                        cur.pre.next = cur.next;
                        last = last.pre;
                    }
                }
                //删除第一个出现的key，删除直接返回即可
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        //找到对应关键字
        while (cur != null) {
            if (cur.val == key) {
                //需要删除的cur在头节点
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.pre = null;
                    } else {
                        last = null;
                    }
                }else {
                    //删除中间节点
                    if(cur.next != null) {
                        cur.pre.next = cur.next;
                        cur.next.pre = cur.pre;
                    }else {
                        //删除尾节点
                        cur.pre.next = cur.next;
                        last = last.pre;
                    }
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur.pre = null;
            cur = curNext;
        }
        this.head = this.last = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
